public class MyComplex extends Object {
    double RealPart = 0;   //实部
    double ImagePart = 0;   //虚部

    public MyComplex() {
    }

    public MyComplex(double R, double I) {
        RealPart = R;
        ImagePart = I;
    }

    double getRealPart() {
        return RealPart;
    }

    double getImagePart() {
        return ImagePart;
    }

    public boolean equals(Object obj){
        MyComplex b = (MyComplex)obj;
        if(b.ImagePart == this.ImagePart && b.RealPart == this.RealPart){
            return true;
        }
        else {
            return false;
        }
    }

    public String toString(){
        StringBuffer Bf = new StringBuffer();
        if(RealPart!=0&&ImagePart!=0) {
            Bf.append(RealPart);
            Bf.append('+');
            Bf.append('(');
            Bf.append(ImagePart);
            Bf.append('i');
            Bf.append(')');
            String s = new String(Bf);
            return s;
        }
        else if(ImagePart == 0){
            Bf.append(RealPart);
            String s = new String(Bf);
            return s;
        }
        else {
            Bf.append(ImagePart);
            Bf.append('i');
            String s = new String(Bf);
            return s;
        }
    }

    MyComplex ComplexAdd(MyComplex a){
        MyComplex c = new MyComplex(a.RealPart+this.RealPart,a.ImagePart+this.ImagePart);
        return c;
    }

    MyComplex ComplexSub(MyComplex a){
        MyComplex c = new MyComplex(this.RealPart-a.RealPart,this.ImagePart-a.ImagePart);
        return c;
    }

    MyComplex ComplexMulti(MyComplex a){
        MyComplex c = new MyComplex(a.RealPart*this.RealPart-a.ImagePart*this.ImagePart,a.RealPart*this.ImagePart+a.ImagePart*this.RealPart);
        return c;
    }

    MyComplex ComplexDiv(MyComplex a){
        MyComplex c = new MyComplex(
                (a.RealPart*this.RealPart+a.ImagePart*this.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart),
                (a.RealPart*this.ImagePart-a.ImagePart*this.RealPart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart));
        return c;
    }
}

